<template>
  <div class="search">
    <a-drawer :visible="modalVisible" :title="modalTitle" width="880" @close="handleClose">
      <link-form ref="form1" v-if="modalVisible"></link-form>
      <div class="drawer-footer">
        <a-button type="default" @click="handleClose">取消</a-button>&nbsp;
        <a-button type="primary" @click="handleSubmit" :loading="saving">保存</a-button>
      </div>
    </a-drawer>
  </div>
</template>
<script>


  import LinkForm from './link_form'

  export default {
    name: 'drawer',
    props: {},
    data () {
      return {
        saving: false,
        modalVisible: false,
        isEdit: false,
        title: '',
        current: 'form1',
        roleName: '',
        formData: {},
        forms: [
          'form1'
        ]
      }
    },
    methods: {
      handleShow (data) {
        this.isEdit = false
        if (data && data.linkId) {
          this.isEdit = true
          this.formData = data
        } else {
          this.formData = {}
        }
        this.modalVisible = true
        this.handleTabClick('form1')
      },
      handleClose () {
        this.modalVisible = false
      },
      handleSubmit () {
        this.$refs[this.current].handleSubmit()
      },
      handleTabClick (name) {
        this.current = name
        if (this.current === this.forms[0]) {
          this.title = this.isEdit ? '编辑友情链接' : '添加友情链接'
        }
        this.$nextTick(() => {
          this.$refs[this.current].setData(this.formData)
        })
      }
    },
    computed: {
      modalTitle: function () {
        return this.title
      }
    },
    created () {
      //监听modal关闭事件
      this.$bus.on('closeModal', (name) => {
        this.modalVisible = false
      })
    },
    components: { LinkForm }
  }
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
